package com.example.newheze.Current;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.newheze.R;
import com.example.newheze.Util.HttpUtils;
import com.example.newheze.vo.Dtu;
import com.example.newheze.vo.Info;
import com.example.newheze.vo.Show;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class CurrentActivity extends Fragment {
    private ListView listView;
    Spinner spinner;
    private ShowAdapter adapter;
    private Info data=new Info();
    private Dtu dtu=new Dtu();
    Button shuaxin;

    private Show id;
    private Show airtemp1;
    private Show airhumi1;
    private Show soiltemp1;
    private Show soiltemp12;
    private Show soilhumi1;
    private Show soilhumi12;
    private Show soilelect1;
    private  Show soilelect2;
    private  Show sunlight;
    private  Show co2;



    private List<Show> showList = new ArrayList<Show>();
    @Override
    /*public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.currentdata);
        ListView listView = (ListView) findViewById(R.id.listview);
        ShowAdapter adapter = new ShowAdapter(CurrentActivity.this,getData());
        listView.setAdapter(adapter);
    }
*/
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.currentdata, container, false);
        //setContentView(R.layout.currentdata);
        listView = view.findViewById(R.id.listview);
        adapter = new ShowAdapter(this.getActivity(),getNowData(dtu.getDtu()));
        listView.setAdapter(adapter);
//        shuaxin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getData();
//            }
//        });


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public List<Show> getNowData(String dtuid){
        if (dtuid.equals("006") ||dtuid.equals("007")|| dtuid.equals("008")){
            showList.clear();
            id= new Show(dtuid+"号大棚",R.drawable.datatime,"");
            showList.add(id);
            airtemp1 = new Show("空气温度", R.drawable.kongqiwendu,"");
            showList .add(airtemp1);
            airhumi1 = new Show("空气湿度", R.drawable.kongqishidu," ");
            showList.add(airhumi1);
            soiltemp1 = new Show("土壤温度1", R.drawable.turangwendu," ");
            showList.add(soiltemp1);
            soilhumi1  = new Show("土壤湿度1", R.drawable.turangshidu," ");
            showList.add(soilhumi1);

        }else{
            showList.clear();
            id= new Show(dtuid+"号大棚",R.drawable.datatime,"");
            showList.add(id);
            airtemp1 = new Show("空气温度", R.drawable.kongqiwendu,"");
            showList .add(airtemp1);
            airhumi1 = new Show("空气湿度", R.drawable.kongqishidu," ");
            showList.add(airhumi1);
            soiltemp1 = new Show("土壤温度1", R.drawable.turangwendu," ");
            showList.add(soiltemp1);
            soiltemp12= new Show("土壤温度2", R.drawable.turangwendu," ");
            showList.add(soiltemp12);
            soilhumi1  = new Show("土壤湿度1", R.drawable.turangshidu," ");
            showList.add(soilhumi1);
            soilhumi12 = new Show("土壤湿度2", R.drawable.turangshidu," ");
            showList.add(soilhumi12);
            soilelect1= new Show("土壤电导率1", R.drawable.diandaolv," ");
            showList.add(soilelect1);
            soilelect2= new Show("土壤电导率2", R.drawable.diandaolv," ");
            showList.add(soilelect2);
            sunlight= new Show("光照", R.drawable.guangzhao," ");
            showList.add(sunlight);
            co2= new Show("Co2", R.drawable.co2," ");
            showList.add(co2);
        }

        return showList;
    }

    public void getData(String url){
        new MyAsyncTask().execute(url);
    }

    public class MyAsyncTask extends AsyncTask<String, Void, String>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                String myDtu=jsonObject.getString("DTU");
                dtu.setDtu(jsonObject.getString("DTU"));
                if (myDtu.equals("006")||myDtu.equals("007")||myDtu.equals("008")){
                    //Log.d("leotest",myDtu+"*******"+s);
                    data = new Info(jsonObject.getString("datetime"),
                            jsonObject.getString("soil_humi1"),
                            jsonObject.getString("soil_temp1"),
                            "--",
                            "--",
                            "--",
                            "--",
                            "--",
                            "--",
                            jsonObject.getString("air_humi"),
                            jsonObject.getString("air_temp"),
                            myDtu);
                    id.setAdata(data.getDatetime());
                    airtemp1.setAdata(data.getAirtemp()+"℃");
                    airhumi1.setAdata(data.getAirhumi()+"%");
                    soiltemp1.setAdata(data.getSoil_temp1()+"℃");
                    soilhumi1.setAdata(data.getSoil_humi1()+"%");

                }else{
                    data = new Info(jsonObject.getString("datetime"), jsonObject.getString("soil_humi1"), jsonObject.getString("soil_temp1"), jsonObject.getString("soil_elect1"), jsonObject.getString("soil_humi2"), jsonObject.getString("soil_temp2"), jsonObject.getString("soil_elect2"), jsonObject.getString("sunlight"), jsonObject.getString("co2"), jsonObject.getString("air_humi"), jsonObject.getString("air_temp"), jsonObject.getString("DTU"));
                    id.setAdata(data.getDatetime());
                    airtemp1.setAdata(data.getAirtemp()+"℃");
                    airhumi1.setAdata(data.getAirhumi()+"%");
                    soiltemp1.setAdata(data.getSoil_temp1()+"℃");
                    soiltemp12.setAdata(data.getSoil_temp2()+"℃");
                    soilhumi1.setAdata(data.getSoil_humi1()+"%");
                    soilhumi12.setAdata(data.getSoil_humi2()+"%");
                    soilelect1.setAdata(data.getSoil_elect1()+"dS/m");
                    soilelect2.setAdata(data.getSoil_elect2()+"dS/m");
                    sunlight.setAdata(data.getSunlight()+"KLX");
                    co2.setAdata(data.getCo2()+"PPM");
                }
                listView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.sendPostMethod(strings[0],"utf-8");
        }
    }

//    private void getData(){
//        new Thread(new Runnable(){
//           public void run(){
//               try{
//               OkHttpClient client = new OkHttpClient();
//               Request request = new Request.Builder().url("http://202.194.131.174:8080/wudihoutai/Wenshi_wudi_1_servlet?").build();
//               Response response = null;
//               response = client.newCall(request).execute();
//               if(response.isSuccessful()){
//                   JSONObject jsonObject = new JSONObject(response.toString());
//                   refresh(jsonObject);
//                }
//               }catch(Exception e){
//                   e.printStackTrace();
//                }
//
//           }
//        });
//    }
//
//
//    private void refresh(final JSONObject jsonObject){
//       getActivity().runOnUiThread(new Runnable(){
//           public void run(){
//               FragmentManager fragmentManager = getFragmentManager();
//                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//               try {
//                   data = new Info(jsonObject.getString("id"), jsonObject.getString("datetime"), jsonObject.getString("soil_humi1"), jsonObject.getString("soil_temp1"), jsonObject.getString("soil_elect1"), jsonObject.getString("soil_humi2"), jsonObject.getString("soil_temp2"), jsonObject.getString("soil_elect2"), jsonObject.getString("sunlight"), jsonObject.getString("co2"), jsonObject.getString("air_humi"), jsonObject.getString("air_temp"), jsonObject.getString("DTU"));
//               } catch (JSONException e) {
//                   e.printStackTrace();
//               }
//               airtemp1.setAdata(data.getAirtemp());
//               airhumi1.setAdata(data.getAirhumi());
//               soiltemp1.setAdata(data.getSoil_temp1());
//               soiltemp12.setAdata(data.getSoil_temp2());
//               soilhumi1.setAdata(data.getSoil_humi1());
//               soilhumi12.setAdata(data.getSoil_humi2());
//               soilelect1.setAdata(data.getSoil_elect1());
//               soilelect2.setAdata(data.getSoil_elect2());
//               sunlight.setAdata(data.getSunlight());
//               co2.setAdata(data.getCo2());
//               listView.setAdapter(adapter);
//               }
//       });
//    }'


    @SuppressLint("ValidFragment")
    public CurrentActivity(String mydtu) {
        dtu.setDtu(mydtu);
    }

    public CurrentActivity() {
    }
}


